package cn.iocoder.yudao.module.erp.controller.admin.stock.bom;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.stock.bom.vo.BomPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.stock.bom.vo.BomRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.stock.bom.vo.BomSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.stock.bom.BomDO;
import cn.iocoder.yudao.module.erp.service.stock.bom.BomService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - ERP BOM")
@RestController
@RequestMapping("/erp/bom")
@Validated
public class BomController {
    @Resource
    private BomService bomService;

    @PostMapping("/create")
    @Operation(summary = "创建BOM")
    @PreAuthorize("@ss.hasPermission('erp:bom:create')")
    public CommonResult<Long> createBom(@Valid @RequestBody BomSaveReqVO createReqVO) {
        return success(bomService.createBom(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新BOM")
    @PreAuthorize("@ss.hasPermission('erp:bom:update')")
    public CommonResult<Boolean> updateBom(@Valid @RequestBody BomSaveReqVO updateReqVO) {
        bomService.updateBom(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除BOM")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:bom:delete')")
    public CommonResult<Boolean> deleteBom(@RequestParam("id") Long id) {
        bomService.deleteBom(id);
        return success(true);
    }

    @DeleteMapping("/delete-any")
    @Operation(summary ="批量删除BOM")
    @Parameter(name = "ids", description = "ids",required = true)
    @PreAuthorize("@ss.hasPermission('erp:bom:delete')")
    public CommonResult<Boolean> deleteAnyBOM(@RequestParam("ids") List<Long> ids) {
        bomService.deleteAnyBOM(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得BOM")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:bom:query')")
    public CommonResult<BomRespVO> getBom(@RequestParam("id") Long id) {
        BomDO bom = bomService.getBom(id);
        return success(BeanUtils.toBean(bom, BomRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得BOM分页")
    @PreAuthorize("@ss.hasPermission('erp:bom:query')")
    public CommonResult<PageResult<BomRespVO>> getBomPage(@Valid BomPageReqVO pageReqVO) {
        PageResult<BomDO> pageResult = bomService.getBomPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BomRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出BOM Excel")
    @PreAuthorize("@ss.hasPermission('erp:bom:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBomExcel(@Valid BomPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BomDO> list = bomService.getBomPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "BOM.xls", "数据", BomRespVO.class,
                        BeanUtils.toBean(list, BomRespVO.class));
    }

}